package com.siashan.toolkit.sms.jdcloud.core;

import com.jdcloud.sdk.auth.CredentialsProvider;
import com.jdcloud.sdk.auth.StaticCredentialsProvider;
import com.jdcloud.sdk.http.HttpRequestConfig;
import com.jdcloud.sdk.http.Protocol;
import com.jdcloud.sdk.service.sms.client.SmsClient;
import com.jdcloud.sdk.service.sms.model.BatchSendRequest;
import com.jdcloud.sdk.service.sms.model.BatchSendResult;
import com.siashan.toolkit.sms.core.entity.*;
import com.siashan.toolkit.sms.core.executor.AbstractSmsSendExecutor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Map;

/**
 * 京东云短信通知执行器
 *
 * @author siashan
 * @since v1.0.1
 **/
@Slf4j
public class JdCloudSmsSendExecutor extends AbstractSmsSendExecutor {

    /**
     * 短信客户端
     */
    private final SmsClient smsClient;


    /**
     * 阿里云短信配置
     */
    private JdCloudSmsConfig jdCloudSmsConfig;

    /**
     * 构造京东云短信发送处理
     *
     * @param config 京东云短信配置
     */
    public JdCloudSmsSendExecutor(JdCloudSmsConfig config) {
        super(config);
        this.jdCloudSmsConfig = config;
        CredentialsProvider credentialsProvider = new StaticCredentialsProvider(config.getAccessKeyId(), config.getSecretAccessKey());
        smsClient = SmsClient.builder().credentialsProvider(credentialsProvider)
                .httpRequestConfig(new HttpRequestConfig.Builder().protocol(Protocol.HTTP).build()).build();
    }

    /**
     * 构造京东云短信发送处理
     *
     * @param config 京东云短信配置
     */
    public JdCloudSmsSendExecutor(JdCloudSmsConfig config, Map<String, SmsTemplate> templateMap) {
        this(config);
        this.templateMap = templateMap;
    }

    /**
     * 构造京东云短信发送处理
     *
     * @param config 京东云短信配置
     */
    public JdCloudSmsSendExecutor(JdCloudSmsConfig config, Map<String, SmsTemplate> templateMap,SmsWhiteList whiteList) {
        this(config,templateMap);
        this.whiteList = whiteList;
    }

    /**
     * 构造京东云短信发送处理
     *
     * @param config 京东云短信配置
     */
    public JdCloudSmsSendExecutor(JdCloudSmsConfig config, Map<String, SmsTemplate> templateMap,SmsWhiteList whiteList,SmsFrequencyLimitConfig frequencyLimit) {
        this(config,templateMap,whiteList);
        this.frequencyLimit = frequencyLimit;
    }

    /**
     * 发送批量通知
     *
     * @param smsSendModel 通知参数
     * @return 是否发送成功
     */
    @Override
    public boolean doSend(SmsModel smsSendModel) {
        BatchSendRequest request = new BatchSendRequest();
        request.setRegionId(jdCloudSmsConfig.getRegion());
        request.setTemplateId(smsSendModel.getTemplateId());
        request.setSignId(jdCloudSmsConfig.getSignId());
        request.setPhoneList(new ArrayList<>(smsSendModel.getPhones()));
        Map<String, String> params = smsSendModel.getParams();
        if (null != params && !params.isEmpty()) {
            request.setParams(transferParamsWithoutKey(params, null));
        }
        BatchSendResult result = smsClient.batchSend(request).getResult();
        Boolean status = result.getStatus();
        boolean flag = status != null && status;

        return flag;
    }

    /**
     * 获取通道
     *
     * @return 通道
     */
    @Override
    public SmsChannel getChannel() {
        return SmsChannel.JD;
    }

}
